<template>
	<!--商品评价-->
	<div class="foodRatings">
		<div class="ranting-type">
			<span @click="select(2,$event)" class="block" :class="{'active': selecttype==2}">{{desc.all}}<span class="count">{{ratings.length}}</span></span>
			<span  @click="select(0,$event)" class="block positive" :class="{'active': selecttype==0}">{{desc.positive}}<span class="count">{{positives.length}}</span></span>
			<span  @click="select(1,$event)" class="block negative" :class="{'active': selecttype==1}">{{desc.negative}}<span class="count">{{negatives.length}}</span></span>
		</div>
		<div @click="toggleContent($event)" class="ranting-switch clearfix">
			<div class="icon-switch fl" :class="{'on':onlycontent}"></div>
			<div class="text fl">只看内容的评价</div>
		</div>
	</div>
</template>

<script>
	const POSITIVE = 0;/*积极*/
	const NEGATIVE = 1;/*消极*/
	const ALL = 2;/*所有*/
	/* ratings-评论数据
	 * desc-评论的三种状态描述(是固定的)
	 * selectType-选择的评论状态0,1,2
	 * onlyContent-是否是只看有内容的评论
	 * 
	 * selectType、onlyContent这两个状态是用户选择的(有默认值)，
	 * 一旦选择，实现进入到所有的食物详情页的时候都是这种选择，避免重复操作
	 * 所以，子组件选择的数据是需要传递给父组件的
	 * */
	export default{
		props:{
			ratings:{
				type: Array,
				default(){
					return [];
				}
			},
			selectType:{
				type: Number,
				default: ALL
			},
			onlyContent:{
				type: Boolean,
				default: false
			},
			desc:{
				type: Object,
				default(){
					return {
						all: '全部',
						positive: '满意',
						negative: '不满意'
					}
				}
			}
		},
		data(){
			return{
				selecttype: ALL,
				onlycontent: false 
			}
		},
		created(){
			this.selecttype = this.selectType;
			this.onlycontent = this.onlyContent;
		},
		computed:{
			positives(){
				let arr = [];
				this.ratings.forEach(function(v){
					if(v.rateType==0){
						arr.push(v);
					}
				})
				return arr;
			},
			negatives(){
				let arr = [];
				this.ratings.forEach(function(v){
					if(v.rateType==1){
						arr.push(v);
					}
				})
				return arr;
			}
		},
		
		methods:{
			select(selecttype,event){
				if(!event._constructed){
					return;
				}
				this.selecttype = selecttype;
				this.$emit('stype',this.selecttype);
			},
			toggleContent(event){
				if(!event._constructed){
					return;
				}
				this.onlycontent = !this.onlycontent;
				this.$emit('togContent',this.onlycontent);
			}
		}
	}
</script>

<style lang="stylus">
	.ranting-switch{
		padding: 0 18px;
		border-bottom: 1px solid rgba(7,17,27,0.1);
		.icon-switch{
			margin: 12px 0;
			width: 20px;
			height: 20px;
			background-image: url(../../assets/select.png);
			background-repeat: no-repeat;
			background-size: contain;
			margin-right: 4px;
			&.on{
				background-image: url(../../assets/selected.fw.png);
			}
		}
		.text{
			line-height: 24px;
			font-size: 12px;
			color: rgb(147,153,159);
			margin-top: 10px;
		}
		
	}
	.ranting-type{
		padding: 18px 0;
		margin: 0 18px;
		border-bottom: 1px solid rgba(7,17,27,0.1);
		font-size: 0;
		.block{
			margin-right: 8px;
			display: inline-block;
			width: 60px;
			height: 32px;
			line-height: 32px;
			text-align: center;
			font-size: 12px;
			border-radius: 2px;
			color: rgb(75,85,93);
			background: rgba(0,160,220,0.2);
			&.active{
				color: #fff;
				background: rgb(0,160,220);
			}
		}
		.negative{
			background:  rgba(75,85,93,0.2);
			&.active{
				background: rgb(75,85,93);
			}
		}
	}
</style>